#include "timer_smg.h"
#include "smg.h"
#include "dht11.h"
//数码管变量
extern uint8_t num1,num2,num3,num4;//4个数码管显示的数值
extern uint8_t num_flag;//4个数码管和冒号轮流显示，⼀轮刷新五次
extern uint8_t humdity_integer;//湿度整数
extern uint8_t temp_integer;//温度整数
//按键变量
extern bsp_io_level_t sw1;//按键SW1状态
extern bsp_io_level_t sw2;//按键SW2状态
extern bsp_io_level_t sw3;//按键SW3状态
extern bsp_io_level_t sw4;//按键SW4状态
extern bsp_io_level_t qe_sw;//触摸电容状态
extern int sw1_num1;//按键SW1计数值，去抖和⻓按短按判断
extern int sw2_num1;//按键SW2计数值，去抖和⻓按短按判断
extern int sw3_num1;//按键SW3计数值，去抖和⻓按短按判断
extern int sw4_num1;//按键SW4计数值，去抖和⻓按短按判断
extern int qe_sw_num1;//触摸按键计数值，去抖和⻓按短按判断
//数码管显示状态，0正常显示，1修改⼩时，2修改分钟，3保存修改数据，4温度，5湿度
extern int smg_mode;
extern int sec,min,hour;//保存时间数据
extern uint16_t time_mode_num;//定时器刷新时间，实现闪烁效果

extern rtc_time_t set_time;//RTC时间定义


void buzzer(void)
{
 /*while(buzzer_num>0)
 {
 if(buzzer_num%40<20)
 R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_03_PIN_02, BSP_IO_LEVEL_HIGH) ;
 else
 R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_03_PIN_02, BSP_IO_LEVEL_LOW) ;
 buzzer_num--;
 }*/
 /*                                2700/4=675                            */

 for(int i=0;i<675;i++)
 {
     R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_03_PIN_02, BSP_IO_LEVEL_HIGH) ;
     R_BSP_SoftwareDelay(110U, BSP_DELAY_UNITS_MICROSECONDS);
     R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_03_PIN_02, BSP_IO_LEVEL_LOW) ;
     R_BSP_SoftwareDelay(110U, BSP_DELAY_UNITS_MICROSECONDS);
 }
 for(int i=0;i<675;i++)
  {
      R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_03_PIN_02, BSP_IO_LEVEL_HIGH) ;
      R_BSP_SoftwareDelay(135U, BSP_DELAY_UNITS_MICROSECONDS);
      R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_03_PIN_02, BSP_IO_LEVEL_LOW) ;
      R_BSP_SoftwareDelay(135U, BSP_DELAY_UNITS_MICROSECONDS);
  }
 for(int i=0;i<675;i++)
   {
       R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_03_PIN_02, BSP_IO_LEVEL_HIGH) ;
       R_BSP_SoftwareDelay(160U, BSP_DELAY_UNITS_MICROSECONDS);
       R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_03_PIN_02, BSP_IO_LEVEL_LOW) ;
       R_BSP_SoftwareDelay(160U, BSP_DELAY_UNITS_MICROSECONDS);
   }
 for(int i=0;i<675;i++)
   {
       R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_03_PIN_02, BSP_IO_LEVEL_HIGH) ;
       R_BSP_SoftwareDelay(185U, BSP_DELAY_UNITS_MICROSECONDS);
       R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_03_PIN_02, BSP_IO_LEVEL_LOW) ;
       R_BSP_SoftwareDelay(185U, BSP_DELAY_UNITS_MICROSECONDS);
   }



}

void play_music(void)
{
    //do
        for(int i=0;i<262/2;i++)
           {
               R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_03_PIN_02, BSP_IO_LEVEL_HIGH) ;
               R_BSP_SoftwareDelay(1900U, BSP_DELAY_UNITS_MICROSECONDS);
               R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_03_PIN_02, BSP_IO_LEVEL_LOW) ;
               R_BSP_SoftwareDelay(1900U, BSP_DELAY_UNITS_MICROSECONDS);
           }
       //re
        for(int i=0;i<294/2;i++)
               {
                   R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_03_PIN_02, BSP_IO_LEVEL_HIGH) ;
                   R_BSP_SoftwareDelay(1700U, BSP_DELAY_UNITS_MICROSECONDS);
                   R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_03_PIN_02, BSP_IO_LEVEL_LOW) ;
                   R_BSP_SoftwareDelay(1700U, BSP_DELAY_UNITS_MICROSECONDS);
               }
        //mi
        for(int i=0;i<330/2;i++)
               {
                   R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_03_PIN_02, BSP_IO_LEVEL_HIGH) ;
                   R_BSP_SoftwareDelay(1515U, BSP_DELAY_UNITS_MICROSECONDS);
                   R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_03_PIN_02, BSP_IO_LEVEL_LOW) ;
                   R_BSP_SoftwareDelay(1515U, BSP_DELAY_UNITS_MICROSECONDS);
               }
        //fa
        for(int i=0;i<(349+1)/2;i++)
               {
                   R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_03_PIN_02, BSP_IO_LEVEL_HIGH) ;
                   R_BSP_SoftwareDelay(1433U, BSP_DELAY_UNITS_MICROSECONDS);
                   R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_03_PIN_02, BSP_IO_LEVEL_LOW) ;
                   R_BSP_SoftwareDelay(1433U, BSP_DELAY_UNITS_MICROSECONDS);
               }
        //so
        for(int i=0;i<392/2;i++)
               {
                   R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_03_PIN_02, BSP_IO_LEVEL_HIGH) ;
                   R_BSP_SoftwareDelay(1275U, BSP_DELAY_UNITS_MICROSECONDS);
                   R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_03_PIN_02, BSP_IO_LEVEL_LOW) ;
                   R_BSP_SoftwareDelay(1275U, BSP_DELAY_UNITS_MICROSECONDS);
               }
        //la
        for(int i=0;i<440/2;i++)
               {
                   R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_03_PIN_02, BSP_IO_LEVEL_HIGH) ;
                   R_BSP_SoftwareDelay(1136U, BSP_DELAY_UNITS_MICROSECONDS);
                   R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_03_PIN_02, BSP_IO_LEVEL_LOW) ;
                   R_BSP_SoftwareDelay(1136U, BSP_DELAY_UNITS_MICROSECONDS);
               }
        //si
        for(int i=0;i<494/2;i++)
               {
                   R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_03_PIN_02, BSP_IO_LEVEL_HIGH) ;
                   R_BSP_SoftwareDelay(1012U, BSP_DELAY_UNITS_MICROSECONDS);
                   R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_03_PIN_02, BSP_IO_LEVEL_LOW) ;
                   R_BSP_SoftwareDelay(1012U, BSP_DELAY_UNITS_MICROSECONDS);
               }
}

//2ms一次定时中断回调
void timer0_callback(timer_callback_args_t *p_args)
{
 /* TODO: add your own code here */
 if (TIMER_EVENT_CYCLE_END == p_args->event)
 {

     time_mode_num++;
     if(time_mode_num>200)
     time_mode_num=0;

     //处理按键
    set_smg_button();

     if(smg_mode==0)           //正常显示
    {
     if(num_flag==0)
     smg_1(num1);
     else if(num_flag==1)
     smg_2(num2);
     else if(num_flag==2)
     smg_3(num3);
     else if(num_flag==3)
     smg_4(num4);
     }
     //smg_maohao_open(1); //冒号


    else if(smg_mode==1)//修改时间⼩时
      {
      if(time_mode_num<100)                   //200ms全亮
      {
      if(num_flag==0)
      smg_1(hour/10);
      else if(num_flag==1)
      smg_2(hour%10);
      else if(num_flag==2)
      smg_3(min/10);                          //亮灭闪烁->
      else if(num_flag==3)
      smg_4(min%10);
      //else if(num_flag==4)
     // smg_maohao_open(1); //冒号
      }
      else                                    //200ms数码管1、2灭
      {
      if(num_flag==0)
      smg_1_close();
      else if(num_flag==1)
      smg_2_close();
      else if(num_flag==2)
       smg_3(min/10);
       else if(num_flag==3)
       smg_4(min%10);
       //else if(num_flag==4)
      // smg_maohao_open(1); //冒号
       }
       }

       else if(smg_mode==2)//修改时间分钟
       {
       if(time_mode_num<100)
       {
       if(num_flag==0)
       smg_1(hour/10);
       else if(num_flag==1)
       smg_2(hour%10);
       else if(num_flag==2)
       smg_3(min/10);
       else if(num_flag==3)
       smg_4(min%10);
       //else if(num_flag==4)
      // smg_maohao_open(1); //冒号
       }
       else
       {
       if(num_flag==0)
       smg_1(hour/10);
       else if(num_flag==1)
       smg_2(hour%10);
       else if(num_flag==2)
       smg_3_close();
       else if(num_flag==3)
       smg_4_close();
       //else if(num_flag==4)
       //smg_maohao_open(1); //冒号
       }
       }
       else if(smg_mode==3)//保存数据
       {
       set_time.tm_sec=sec;
       set_time.tm_min =min;
       set_time.tm_hour =hour ;
       R_RTC_CalendarTimeSet(&g_rtc0_ctrl, &set_time);
       smg_mode=0;
       }



     num_flag++;
     if(num_flag==4)
     num_flag=0;

 }
}


void set_smg_button(void)
{
 R_IOPORT_PinRead(&g_ioport_ctrl, BSP_IO_PORT_02_PIN_07, &sw1);
 R_IOPORT_PinRead(&g_ioport_ctrl, BSP_IO_PORT_02_PIN_08, &sw2);
 R_IOPORT_PinRead(&g_ioport_ctrl, BSP_IO_PORT_09_PIN_13, &sw3);
 R_IOPORT_PinRead(&g_ioport_ctrl, BSP_IO_PORT_09_PIN_14, &sw4);
 if(sw1+sw2+sw3+sw4==4&&qe_sw==0)//按键都没按下,清除按键按下计数值
 {
 sw1_num1=0;
 sw2_num1=0;
 sw3_num1=0;
 sw4_num1=0;
 qe_sw_num1=0;
 }
 else if(sw1==0&&sw2&&sw3&&sw4&&qe_sw==0)//只有SW1按下
 {/*
 if(sw1_num1<1001)//按下⼩于10s,1001是防⽌变量在1000时候⼀直切换模式
 sw1_num1++;
 if(sw1_num1%200==0)//模式切换,按下
 {
 //buzzer_num=20;//蜂鸣器叫200ms
 if(smg_mode>2)
 smg_mode=0;
 else
 smg_mode++;
 }
 if(smg_mode==6)
 smg_mode=0;
*/
      smg_mode++;
      while(sw1==0)
      R_IOPORT_PinRead(&g_ioport_ctrl, BSP_IO_PORT_02_PIN_07, &sw1);
      if(smg_mode>3)
      smg_mode=0;
 }

 else if(sw2==0&&sw1&&sw3&&sw4&&qe_sw==0)//只有SW2按下
 {
 //if(sw2_num1<30)//300ms 减⼀次
 if(sw2_num1<60)
 sw2_num1++;
 else
 {
 sw2_num1=0;
 if(smg_mode==1)                           //修改小时
 {
 if(hour>0)
 hour--;
 else
 hour=23;
 }
  else if(smg_mode==2)                     //修改分钟
  {
  if(min>0)
  min--;
  else
  min=59;
  }
  }
  }
  else if(sw3==0&&sw1&&sw2&&sw4&&qe_sw==0)//只有SW3按下
  {
 // if(sw3_num1<30)//300ms 加⼀次
  if(sw3_num1<60)
  sw3_num1++;
  else
  {
  sw3_num1=0;
  if(smg_mode==1)                          //修改小时
  {
  if(hour<23)
  hour++;
  else
  hour=0;
  }
  else if(smg_mode==2)                   //修改分钟
  {
  if(min<59)
  min++;
  else
  min=0;
  }
  }
  }
  else if(sw4==0&&sw1&&sw2&&sw3&&qe_sw==0)//只有SW4按下
  { //打印一次温湿度
   DHT11_Read();
   for(int i=0;i<10;i++)
   for(int j=0;j<25;j++)
   {
   show_1_p();
   R_BSP_SoftwareDelay(4U, BSP_DELAY_UNITS_MILLISECONDS);
   smg_3(temp_integer/10);
   R_BSP_SoftwareDelay(4U, BSP_DELAY_UNITS_MILLISECONDS);
   smg_4(temp_integer%10);
   R_BSP_SoftwareDelay(4U, BSP_DELAY_UNITS_MILLISECONDS);
   }

   for(int i=0;i<10;i++)
   for(int j=0;j<25;j++)
    {
     show_1_h();
     R_BSP_SoftwareDelay(4U, BSP_DELAY_UNITS_MILLISECONDS);
     smg_3(humdity_integer/10);
     R_BSP_SoftwareDelay(4U, BSP_DELAY_UNITS_MILLISECONDS);
     smg_4(humdity_integer%10);
     R_BSP_SoftwareDelay(4U, BSP_DELAY_UNITS_MILLISECONDS);
      }
  }

}


